package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.*;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.report.service.IMergeStudentRegDetailService;
import com.yuyou.fn.util.Webs;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.*;

/**
 * Created by wens on 16-11-29.
 */
@Controller
@RequestMapping("/api/admin/apply")
public class AdminApplyApiController extends ApiBaseController {

    private final static Logger log = LoggerFactory.getLogger(AdminApplyApiController.class);

    @Resource
    private IApplyClassService applyClassService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IMergeStudentRegDetailService mergeStudentRegDetailService;


    @ApiOperation("新增或者修改班级申请")
    @RequestMapping(value = "/saveOrUpdateApplyClass" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> saveOrUpdateApplyClass(@Valid @ModelAttribute ApplyClassSaveOrUpdateVo applyClassSaveOrUpdateVo) {
        applyClassService.saveOrUpdateApplyClass(applyClassSaveOrUpdateVo);
        return  ReturnResult.success("ok");
    }

    @ApiOperation("单个班级详情")
    @RequestMapping(value = "/applyClassDetail" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> applyClassDetail(@RequestParam(value = "applyClassId",required = true)long applyClassId) {

        return  ReturnResult.success(applyClassService.findApplyClassDetail(applyClassId));
    }


    @ApiOperation("班级申请列表")
    @RequestMapping(value = "/findApplyClassDetailPage",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ApplyClassDetailVo>> findApplyClassDetailPage(@Valid @ModelAttribute ApplyClassQueryVo applyClassQueryVo, @RequestParam("pageNo") int pageNo, @RequestParam("pageSize")int pageSize, HttpServletRequest request) {
        DataPermission dataPermission=this.getDataPermission(request);
        if (applyClassQueryVo.getEndDate()!=null){
            applyClassQueryVo.setEndDate(TimeUtils.changeOneDateLastTime(applyClassQueryVo.getEndDate()));
        }
        Page<ApplyClassDetailVo> applyClassDetailPage = applyClassService.findApplyClassDetailPage(dataPermission,applyClassQueryVo, pageNo, pageSize);
        return  ReturnResult.success(applyClassDetailPage);
    }

    @ApiOperation("导出班级申请列表")
    @RequestMapping(value = "/exportApplyClassDetailList",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ApplyClassDetailVo>> exportApplyClassDetailList(@Valid @ModelAttribute ApplyClassQueryVo applyClassQueryVo, HttpServletRequest request, HttpServletResponse response)  throws IOException {
        DataPermission dataPermission=this.getDataPermission(request);
        if (applyClassQueryVo.getEndDate()!=null){
            applyClassQueryVo.setEndDate(TimeUtils.changeOneDateLastTime(applyClassQueryVo.getEndDate()));
        }
        Webs.writeExcel(request, response, applyClassService.exportApplyClassDetail(dataPermission, applyClassQueryVo), "班级申请列表");
        return  ReturnResult.success("ok");
    }

    @ApiOperation("审核不通过")
    @RequestMapping(value = "/auditFailureApplyClass",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ApplyClassDetailVo>> auditFailureApplyClass(@RequestParam("applyClassId")long applyClassId,
                                                                         @RequestParam(value = "auditReason",required = false) String auditReason
                                                                         ) {
        applyClassService.auditFailureApplyClass(applyClassId,auditReason);
        return  ReturnResult.success("ok");
    }


    @ApiOperation("审核通过")
    @RequestMapping(value = "/auditSuccessApplyClass",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> auditSuccessApplyClass(@RequestParam("applyClassId")long applyClassId, @NotEmpty @RequestParam(value = "auditReason",required = false) String auditReason) {
        applyClassService.auditSuccessApplyClass(applyClassId,auditReason);
        return  ReturnResult.success("ok");
    }



    @ApiOperation("审核通过生成班级")
    @RequestMapping(value = "/createClassByApply",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Page<ApplyClassDetailVo>> auditSuccessApplyClass(@Valid @ModelAttribute FullClassSaveVo fullClassSaveVo,
                                                                         @RequestParam("applyClassId")long applyClassId
                                                                         ) {
        applyClassService.createClassByApply(applyClassId,fullClassSaveVo);
        return  ReturnResult.success("ok");
    }


    @ApiOperation("基础指标")
    @RequestMapping(value = "/findRegNumClassNum" ,method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ReturnResult<Void> findRegNumClassNum(@RequestParam("periodId")long periodId,
                                                @RequestParam("busTeamId")long busTeamId,
                                                @RequestParam("gradeId")long gradeId,
                                                @RequestParam("subjectId")long subjectId,
                                                @RequestParam("level")int level
                                                ) {
        List<CourseClass> courseClasses = courseClassService.findSameClass(periodId, busTeamId, gradeId, subjectId, level, "classId");
        List<Long> classIds = BeanUtils.toList(courseClasses, "classId");
        Long regNum=mergeStudentRegDetailService.countReadByClassIds(classIds);
        Map<String,Integer> map=new HashMap<>(2);
        map.put("classNum",courseClasses.size());
        map.put("regNum",regNum.intValue());
        return  ReturnResult.success(map);
    }


}